// 积分改变api
//记录 更新
let {
    user
} = require("../login/sign_schma");
let {
    integralAward
} = require("../integral/awaed_schema");
module.exports = {
    // eslint-disable-next-line no-unused-vars
    integralChange: function (req, res, err) {
        let reqData = req.body;
        console.log(reqData, "积分请求");
        //用户名
        let userQ = reqData.user;
        // 原积分数量
        let numberQ = Number(reqData.number);
        // 改变的积分数量
        let changeQ = Number(reqData.changeNumber);
        // 事件创建时间        
        let timeQ = reqData.createTime;
        //类型 比如：登陆奖励
        let typeQ = reqData.type;

        
        user.updateOne({
            user: userQ
        }, {
            $set: {
                integral: numberQ + changeQ,
                award:true
            }
        }).then((data) => {
            // console.log(data,"integral");
            integralAward.find({
                user: userQ
            }).then((d0) => {
                if (d0.length == 0) {
                    integralAward.create({
                        user: userQ,
                        changeIntegral: changeQ,
                        caseOf: typeQ,
                        total: numberQ + changeQ,
                        time: timeQ
                    }).then((d1) => {
                        // console.log(d1,"创建成功");
                    })
                } else {
                    let changeIntegral = d0[0].changeIntegral;
                    let caseOf = d0[0].caseOf;
                    let total = d0[0].total;
                    let time = d0[0].time;
                    changeIntegral.push(changeQ);
                    total.push(numberQ + changeQ);
                    time.push(timeQ);
                    caseOf.push(typeQ);
                    integralAward.updateOne({
                        user: userQ
                    }, {
                        $set: {
                            changeIntegral,
                            caseOf,
                            time,
                            total
                        }
                    }).then(() => {
                        // console.log("我更新了");
                    })
    
                }
            })

        })
        res.send(true);
    }
}